package org.lq.com.yt.dao.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.lq.com.wjf.entity.Source;
import org.lq.com.xiedi.util.DateUtils;
import org.lq.com.xiedi.util.JDBCUtil;
import org.lq.com.yt.communal.EntityToArray;
import org.lq.com.yt.dao.ProducttypeDao;
import org.lq.com.yt.entity.producttype;

public class ProducttypeDaoImpl implements ProducttypeDao {
	private Connection conn;
	private PreparedStatement ps;
	private ResultSet rs;
	@Override
	public producttype getproducttypeById(int id) {
		producttype ptype=null;
		conn=JDBCUtil.getConn();
		try {
			ps=conn.prepareStatement("select * from producttype where id=?");
			ps.setInt(1, id);
			rs=ps.executeQuery();
			if (rs.next()) {
				ptype=producttype.builder()
						.id(rs.getInt("id"))
						.producttype(rs.getString("producttype"))
						.build();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.closeAll(ps, rs, conn);
		}
		return ptype;
	}

	@Override
	public List<EntityToArray> findAll() {

		producttype ptyp=null;
		List<EntityToArray> list=new ArrayList<EntityToArray>();
		String sql="select * from producttype";
		conn=JDBCUtil.getConn();
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				ptyp = producttype.builder()
						.id(rs.getInt("id"))
						.producttype(rs.getString("producttype"))
						.build();
				list.add(ptyp );
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}


		return list;
	}

	@Override
	public int addtype(producttype pro) {
		String sql= "insert into producttype values(null,?,?)";
		int num = 0;
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1,pro.getProducttype());
			ps.setInt(2, pro.getStatus());

			num = ps.executeUpdate();

		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.closeAll(ps, conn);
		}

		return num;
	}

	@Override
	public int deletetype(int id) {
		String sql = "update producttype set status=0 where id =?";
		int num= JDBCUtil.executeUpdate(sql, id);
		return num;
	}

}
